package com.example.stroebackstage.servlet;

import com.example.stroebackstage.entity.Order;
import com.example.stroebackstage.service.OrderService;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

public class OrderServlet extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("utf-8");
        String toDo = req.getServletPath();
        String toUrl = "/order.jsp";
//        展示全部
        if (toDo.equals("/toShowAllOrder")){
            ArrayList<Order> orderList = new OrderService().selectAll();
            req.getSession().setAttribute("orderList", orderList);
        }
//        展示详情
        if (toDo.equals("/toShowOrderDetail")){
            int orderId = Integer.parseInt(req.getParameter("orderId"));
            Order orderDetail = new OrderService().selectById(orderId).get(0);
            req.getSession().setAttribute("orderDetail",orderDetail);
            toUrl="/detail.jsp";
        }
//        查询
        if (toDo.equals("/toFindOrder")){
            String by = req.getParameter("by");
            String massage = req.getParameter("search");
            if ("OID".equals(by)){
                ArrayList<Order> orderList = new OrderService().selectById(Integer.parseInt(massage));
                req.getSession().setAttribute("orderList", orderList);
            }
            if ("CID".equals(by)){
                ArrayList<Order> orderList = new OrderService().selectByCommodityId(Integer.parseInt(massage));
                req.getSession().setAttribute("orderList", orderList);
            }
            if ("name".equals(by)){
                ArrayList<Order> orderList = new OrderService().selectByCustomerEmail(massage);
                req.getSession().setAttribute("orderList", orderList);
            }
        }
//        删除
        if (toDo.equals("/toDelOrder")){
            int orderId = Integer.parseInt(req.getParameter("orderId"));
            boolean check = new OrderService().del(orderId);
            if (check){
                ArrayList<Order> orderList = new OrderService().selectAll();
                req.getSession().setAttribute("orderList", orderList);
            }
        }
//        修改
        if (toDo.equals("/toUpdateOrder")){
            int id = Integer.parseInt(req.getParameter("id"));
            String name = req.getParameter("name");
            String PAC = req.getParameter("PAC");
            String address = req.getParameter("address");
            String phone = req.getParameter("tel");
            Order order = new Order(id,name,PAC,address,phone);
            if (new OrderService().updateOrder(order)){
                ArrayList<Order> orderList = new OrderService().selectAll();
                req.getSession().setAttribute("orderList", orderList);
            }
        }
//        重定向页面
        resp.sendRedirect(req.getContextPath()+toUrl);
    }
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        doGet(req,resp);
    }
}
